/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package battle.ship.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 *
 * @author Carlos
 */
public abstract class Ship {
    protected int size;//tamanho de barco
    protected List<Point> points;//lista de coordenadas do barco
    protected ShipOrientation oriention;//v-vertical, h-horizontal
    protected Point tempPoint;
    protected int boardSize;

    /**
     *
     * @return
     */
    public abstract String getName();

    /**
     *
     */
    public Ship() {
        this.oriention = ShipOrientation.V;
    }
    
    public Ship(List<Point> points){
        if (points != null){
            if (this.size == points.size()){
                this.points = points;
            } else {
                this.points =new ArrayList<>(this.size);
            }
        } else {
            this.points = new ArrayList<>(this.size);
        }
    }
    
    public List<Point> getPoints() {
        return points;
    }

    public int getSize() {
        return size;
    }

    public ShipOrientation getOriention() {
        return oriention;
    }

    public void setPoints(List<Point> points) {
        this.points = points;
        setOrientation();
    }

    public void setSize(int size) {
        this.size = size;
    }
    
    
    public boolean addPoint(Point point){
        setOrientation();
        if (this.points.contains(point)){
            return false;
        }
        if (this.size > this.points.size()){
            return this.points.add(point);
        }
        return false;
    }
    
    private void setOrientation(){
        if (this.points.size() > 2){
            Point auxPoint = this.points.get(0).subtract(this.points.get(1));
            if (auxPoint.getX() == 0){
                this.oriention = ShipOrientation.H;
                return;
            } 
        }
        this.oriention = ShipOrientation.V;
    }
    
    public Ship(int boardSize){
        this.boardSize = boardSize;
        Random rnd = new Random();
        if (rnd.nextBoolean()){
            this.oriention = ShipOrientation.H;
        }
        this.oriention = ShipOrientation.V;
        if (this.points == null){
            this.points = new ArrayList<>(this.size);
        }
        if (this.points.isEmpty()){
            this.points.add(Point.Random(this.size));
        }
        while (this.size > this.points.size() + 1){
            this.points.add(this.getNextPoint());
        }
    }
    
    private int maxValue(){
        int result = Integer.MIN_VALUE;
        for (Point point : this.points) {
            if(this.oriention == ShipOrientation.H){
                result = point.getX() > result ? point.getX() : result;
            } else {
                result = point.getY() > result ? point.getY() : result;
            }
        }
        return result;
    }
    
    private int minValue(){
        int result = Integer.MAX_VALUE;
        for (Point point : this.points) {
            if(this.oriention == ShipOrientation.H){
                result = point.getX() < result ? point.getX() : result;
            } else {
                result = point.getY() < result ? point.getY() : result;
            }
        }
        return result;
    }
    
    protected Point getNextPoint(){
        Point point = this.points.get(this.points.size() -1);
        if (this.oriention == ShipOrientation.H){
            if (maxValue() >= this.boardSize){
                return new Point(minValue() - 1 , point.getY());
            } else {
                return new Point(maxValue() + 1 , point.getY());
            }
        } else {
            if (maxValue() >= this.boardSize){
                return new Point(point.getX() , minValue() - 1);
            } else {
                return new Point(point.getX() , maxValue() + 1);
            }
        }
    }
}
